############################################################
# What do people think about government, public, efficacy
# of social distancing, etc.? (these are post-treatment outcomes, 
# though noting that info treatments had precisely null effects).
# 
# Author: 
# Soubhik Barari
# 
# Environment:
# - must use R 3.6
# 
# Input:
# - 00-read_clean_data.R
#
# Output:
# - fig/govt_opinion_x_behavior{.png,.pdf}
# - fig/govt_opinion_x_belief{.png,.pdf}
# - fig/reaction_public{.png,.pdf}
# - fig/reaction_govt{.png,.pdf}
# - fig/govt_trust{.png,.pdf}
# - fig/govt_truth{.png,.pdf}
############################################################

source("00-read_clean_data.R")

table(responses_df$think_effec) %>% prop.table()
    #  effective        neutral  not effective 
    # 0.77930622     0.13098086     0.08971292 

table(responses_df$think_publ_extreme) %>% prop.table()
   #   extreme  appropriate insufficient 
   # 0.1085526    0.3606459    0.5308014 

table(responses_df$think_govt_extreme) %>% prop.table()
   #    extreme   appropriate  insufficient 
   # 0.07595694    0.62320574    0.30083732 

table(responses_df$trust_govt) %>% prop.table()
#      high   neutral       low 
# 0.5194961 0.2423515 0.2381524 

table(responses_df$factu_govt) %>% prop.table()
 #  truthful    neutral untruthful 
 # 0.5886228  0.2374251  0.1739521 

#TODO: have trust in govt above other rows

#----------------------------------------------------------
# descriptive: govt/public attitudes x safety beliefs
#----------------------------------------------------------

b_long_df <- responses_df %>%
    tidyr::gather("belief", "value", c("social", "handshake", "stores", "curfew")) %>%
    mutate(belief = 
               ifelse(belief == "social", "Should cancel\nsocial\ngatherings",
                      ifelse(belief == "handshake", "Should avoid\nhandshakes",
                             ifelse(belief == "stores", "Should close\nnon-essential\nshops",
                                    ifelse(belief == "curfew", "Should be a\ngeneral\ncurfew", "")
                                    )))) %>%
    mutate(value = ifelse(value == "Yes", 1, 0))

library(scales)
b_long_df %>% 
    tidyr::gather("variable", "variable_value", c("think_effec", "think_publ_extreme", "think_govt_extreme", "trust_govt", "factu_govt")) %>%
    select(variable, variable_value, belief, value) %>%
    mutate(variable = 
               ifelse(variable == "think_effec", "social\ndistancing is...",
                      ifelse(variable == "think_publ_extreme", "public reaction\nis...",
                             ifelse(variable == "think_govt_extreme", "govt reaction\nis...", 
                                    ifelse(variable == "trust_govt", "trust in\nthe govt...",
                                           ifelse(variable == "factu_govt", "govt COVID-19\ninfo is...", "")
                                    ))))) %>%
    mutate(variable_value = factor(variable_value, levels=c(
        "untruthful", "neutral", "truthful", 
        " extreme", " appropriate", " insufficient",
        "extreme", "appropriate", "insufficient", 
        " not effective", " neutral", " effective",
        "  low", "  neutral", "  high"
    ))) %>%
    mutate(variable = factor(variable, levels=c(
        "trust in\nthe govt...", "govt COVID-19\ninfo is...",
        "social\ndistancing is...", "govt reaction\nis...",
        "public reaction\nis..."
    ))) %>%
    group_by(variable, variable_value, belief) %>%
    summarise(
        y = mean(value)*100
    ) %>%
    mutate(negative=factor(ifelse(
        variable_value %in% c("untruthful"," extreme","extreme"," not effective","  low"), 0, 1
    ))) %>%
    filter(!is.na(variable_value) & variable_value != "") %>%
    ggplot(aes(x=variable_value, y=y)) +
    geom_bar(mapping = aes(fill=negative), stat="identity", position = position_dodge(width=0.9)) +
    scale_fill_manual(values=c("red", "gray32")) +
    geom_text(aes(label=sprintf("%0.1f", y), x=variable_value, y=y+4.2), size=3) +
    coord_flip() +
    scale_y_continuous(limits=c(60, 105),oob = rescale_none) +
    facet_grid(variable ~ belief, scales = "free", space = "free") +
    xlab("") + ylab("percentage of respondents") +
    theme_bw() +
    theme(axis.text.x = element_text(angle=45, size=8), legend.position="none")
ggsave("fig/govt_opinion_x_belief.pdf", width=8, height=6)
ggsave("fig/govt_opinion_x_belief.png", width=8, height=6)


#----------------------------------------------------------
# descriptive: govt/public attitudes x behaviors
#----------------------------------------------------------

b_long_df <- responses_df %>%
    tidyr::gather("behavior", "value", c("SelfReported_Behavio_1", "SelfReported_Behavio_2", "SelfReported_Behavio_3", "SelfReported_Behavio_4", "SelfReported_Behavio_5")) %>%
    mutate(value=as.numeric(value)) %>%
    filter(value != 0) %>% ##these are almost entirely unanswered, not actual zeroes
    mutate(behavior = 
               ifelse(behavior == "SelfReported_Behavio_1", "Stayed at home",
                      ifelse(behavior == "SelfReported_Behavio_2", "Avoided social\ngatherings",
                             ifelse(behavior == "SelfReported_Behavio_3", "Washed hands\nmore frequently",
                                    ifelse(behavior == "SelfReported_Behavio_4", "Kept social distance\n of 2m from others",
                                           ifelse(behavior == "SelfReported_Behavio_5", "Inform others\nof any symptoms", "")
                                    )))))
b_long_df$behavior <- factor(b_long_df$behavior, 
                             levels = c(
                                 "Kept social distance\n of 2m from others",
                                 "Washed hands\nmore frequently",
                                 "Stayed at home",
                                 "Avoided social\ngatherings",
                                 "Inform others\nof any symptoms"
                             ))

library(scales)
b_long_df %>% 
    filter(gender != "Other") %>%
    tidyr::gather("variable", "variable_value", c("think_effec", "think_publ_extreme", "think_govt_extreme", "trust_govt", "factu_govt")) %>%
    select(variable, variable_value, behavior, value) %>%
    mutate(variable = 
               ifelse(variable == "think_effec", "social\ndistancing is...",
                      ifelse(variable == "think_publ_extreme", "public reaction\nis...",
                             ifelse(variable == "think_govt_extreme", "govt reaction\nis...", 
                                    ifelse(variable == "trust_govt", "trust in\nthe govt...",
                                           ifelse(variable == "factu_govt", "govt COVID-19\ninfo is...", "")
                             ))))) %>%
    mutate(variable_value = factor(variable_value, levels=c(
        "untruthful", "neutral", "truthful", 
        " extreme", " appropriate", " insufficient",
        "extreme", "appropriate", "insufficient", 
        " not effective", " neutral", " effective",
        "  low", "  neutral", "  high"
    ))) %>%
    mutate(variable = factor(variable, levels=c(
        "trust in\nthe govt...", "govt COVID-19\ninfo is...",
        "social\ndistancing is...", "govt reaction\nis...", "public reaction\nis..."
    ))) %>%
    group_by(variable, variable_value, behavior) %>%
    summarise(
        y = mean(value, na.rm=T),
        ymin = mean(value, na.rm=T)-(1.96*sd(value, na.rm=T)/sqrt(n())),
        ymax = mean(value, na.rm=T)+(1.96*sd(value, na.rm=T)/sqrt(n()))
    ) %>%
    mutate(negative=factor(ifelse(
        variable_value %in% c("untruthful"," extreme","extreme"," not effective","  low"), 0, 1
    ))) %>%
    filter(!is.na(variable_value) & variable_value != "") %>%
    ggplot(aes(x=variable_value, y=y)) +
    geom_bar(mapping = aes(fill=negative), stat="identity", position = position_dodge(width=0.9)) +
    geom_errorbar(aes(ymin=ymin, ymax=ymax), position = position_dodge(width=0.9), width = 0, size=1) +
    geom_text(aes(label=sprintf("%0.1f", y), x=variable_value, y=ymax+4.2), size=3) +
    coord_flip() +
    scale_fill_manual(values=c("red", "gray32")) +
    scale_y_continuous(limits=c(60, 105),oob = rescale_none) +
    facet_grid(variable ~ behavior, scales = "free", space = "free") +
    xlab("") + ylab("average self-reported grade") +
    theme_bw() +
    theme(axis.text.x = element_text(angle=45, size=8), legend.position="none")
ggsave("fig/govt_opinion_x_behavior.pdf", width=8, height=6)
ggsave("fig/govt_opinion_x_behavior.png", width=8, height=6)


#----------------------------------------------------------
# descriptive: public rxn
#----------------------------------------------------------

q = responses_df$Q23
qdf = data.frame(x=q) %>% group_by(x) %>% summarise(y=n())
qdf$x = factor(qdf$x, levels=rev(unique(qdf$x)))
qdf$y = (qdf$y/sum(qdf$y)) * 100
p <- qdf %>%
    # mutate(t=ifelse(
    #     t == "one", "externalities +\nreflection",
    #     ifelse(
    #         t == "two", "externalities+\nnaming", "control"   
    #     )
    # )) %>%
    filter(x != "") %>%
    ggplot(aes(x=x, y=y)) + 
    geom_bar(stat="identity") + 
    coord_flip() + 
    # facet_grid(.~ t) +
    scale_x_discrete(labels = rev(c("insufficient","somewhat insufficient","appropriate","somewhat extreme","too extreme"))) +
    xlab("public reaction was...") + ylab(sprintf("percentage of responses (n=%i)", nrow(responses_df))) +
    theme_bw()
p
ggsave("fig/reaction_public.pdf", width=4, height=2.5)
ggsave("fig/reaction_public.png", width=4, height=2.5)


#----------------------------------------------------------
# descriptive: govt rxn
#----------------------------------------------------------

q = responses_df$perceivedreaction
# qdf = data.frame(x=q, t=responses_df$treatment) %>% group_by(x, t) %>% summarise(y=n())
qdf = data.frame(x=q, t=responses_df$treatment) %>% group_by(x) %>% summarise(y=n())
qdf$x = factor(qdf$x, levels=rev(unique(qdf$x)))
qdf$y = (qdf$y/sum(qdf$y)) * 100
p <- qdf %>%
    filter(x != "") %>%
    ggplot(aes(x=x, y=y)) + 
    geom_bar(stat="identity") + 
    coord_flip() + 
    # facet_grid(.~ t) +
    scale_x_discrete(labels = rev(c("insufficient","somewhat insufficient","appropriate","somewhat extreme","too extreme"))) +
    xlab("govt reaction was...") + ylab(sprintf("percentage of responses (n=%i)", nrow(responses_df))) +
    theme_bw()
p
ggsave("fig/reaction_govt.pdf", width=4, height=2.5)
ggsave("fig/reaction_govt.png", width=4, height=2.5)


#----------------------------------------------------------
# descriptive: trust in govt
#----------------------------------------------------------

q = responses_df$Q36
qdf = data.frame(x=q, t=responses_df$treatment) %>% group_by(x) %>% summarise(y=n())
qdf$x = factor(qdf$x, levels=rev(qdf$x))
qdf$y = (qdf$y/sum(qdf$y)) * 100
p <- qdf %>%
    filter(x != "") %>%
    ggplot(aes(x=x, y=y)) + 
    geom_bar(stat="identity") + 
    coord_flip() + 
    # facet_grid(.~ t) +
    scale_x_discrete(labels = rev(c("strongly distrust","distrust","neither","trust","strongly trust"))) +
    xlab("trust in govt?") + ylab(sprintf("percentage of responses (n=%i)", nrow(responses_df))) +
    theme_bw()
p
ggsave("fig/govt_trust.pdf", width=4, height=2.5)
ggsave("fig/govt_trust.png", width=4, height=2.5)


#----------------------------------------------------------
# descriptive: govt factually accurate
#----------------------------------------------------------

q = responses_df$Q37
qdf = data.frame(x=q, t=responses_df$treatment) %>% group_by(x) %>% summarise(y=n())
qdf$x = factor(qdf$x, levels=rev(qdf$x))
qdf$y = (qdf$y/sum(qdf$y)) * 100
p <- qdf %>%
    filter(x != "") %>%
    ggplot(aes(x=x, y=y)) + 
    geom_bar(stat="identity") + 
    coord_flip() + 
    # facet_grid(.~ t) +
    scale_x_discrete(labels = rev(c("strongly distrust","distrust","neither","trust","strongly trust"))) +
    xlab("govt been truthful?") + ylab(sprintf("percentage of responses (n=%i)", nrow(responses_df))) +
    theme_bw()
p
ggsave("fig/govt_truth.pdf", width=4, height=2.5)
ggsave("fig/govt_truth.png", width=4, height=2.5)
